package com.addcn.android.design591.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.addcn.android.design591.R;
import com.addcn.android.design591.activity.DesignImageActivity;
import com.addcn.android.design591.activity.TestDetailActivity;
import com.addcn.android.design591.bannerViewPager.BannerViewPager;
import com.addcn.android.design591.bannerViewPager.BannerViewPagerAdapter;
import com.addcn.android.design591.bannerViewPager.OnPageClickListener;
import com.addcn.android.design591.bean.HomeBean;
import com.addcn.android.design591.comm.Configs;
import com.addcn.android.design591.comm.OkHttpConn;
import com.addcn.android.design591.utils.L;
import com.addcn.android.design591.utils.ToolUtils;
import com.bumptech.glide.Glide;
import com.gjiazhe.panoramaimageview.GyroscopeObserver;
import com.gjiazhe.panoramaimageview.PanoramaImageView;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by xuge on 2016/12/30.
 * 首页
 */

public class HomeFragment extends Fragment {
    private Toolbar toolbar;
    private AppBarLayout appbarlayout;
    private ImageView home_img;
    private TextView home_item_title;
    private TextView home_item_more;
    private PanoramaImageView home_item_image_jyf;
    private PanoramaImageView home_item_image_xdf;
    private PanoramaImageView home_item_image_xcf;
    private PanoramaImageView home_item_image_bof;
    private PanoramaImageView home_item_image_xxf;
    private ImageView home_bottom_img_top_left;
    private TextView home_bottom_txt_top_left;
    private ImageView home_bottom_img_top_right;
    private TextView home_bottom_txt_top_right;
    private ImageView home_bottom_img_bottom_left;
    private TextView home_bottom_txt_bottom_left;
    private ImageView home_bottom_img_bottom_right;
    private TextView home_bottom_txt_bottom_right;

    private HomeBean homeBean;
    private BannerViewPager bannerviewpagerview;
    private BannerViewPagerAdapter bannerViewPagerAdapter;

    private ArrayList<ImageView> bannerArrayList = new ArrayList<>();


    private GyroscopeObserver gyroscopeObserver;
    private LinearLayout home_top_item_design;
    private LinearLayout home_top_item_company;
    private LinearLayout home_top_item_knowledge;
    private LinearLayout home_top_item_offer;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_home, null);
        initView(contentView);

        return contentView;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getData();
    }

    private void initView(View contentView) {
        toolbar = (Toolbar) contentView.findViewById(R.id.toolbar);
        appbarlayout = (AppBarLayout) contentView.findViewById(R.id.appbarlayout);

        home_item_title = (TextView) contentView.findViewById(R.id.home_item_title);
        home_item_more = (TextView) contentView.findViewById(R.id.home_item_more);
        home_item_image_jyf = (PanoramaImageView) contentView.findViewById(R.id.home_item_image_jyf);
        home_item_image_xdf = (PanoramaImageView) contentView.findViewById(R.id.home_item_image_xdf);
        home_item_image_xcf = (PanoramaImageView) contentView.findViewById(R.id.home_item_image_xcf);
        home_item_image_bof = (PanoramaImageView) contentView.findViewById(R.id.home_item_image_bof);
        home_item_image_xxf = (PanoramaImageView) contentView.findViewById(R.id.home_item_image_xxf);


        gyroscopeObserver = new GyroscopeObserver();
        home_item_image_jyf.setGyroscopeObserver(gyroscopeObserver);
        home_item_image_xdf.setGyroscopeObserver(gyroscopeObserver);
        home_item_image_xcf.setGyroscopeObserver(gyroscopeObserver);
        home_item_image_bof.setGyroscopeObserver(gyroscopeObserver);
        home_item_image_xxf.setGyroscopeObserver(gyroscopeObserver);


        home_bottom_img_top_left = (ImageView) contentView.findViewById(R.id.home_bottom_img_top_left);
        home_bottom_txt_top_left = (TextView) contentView.findViewById(R.id.home_bottom_txt_top_left);
        home_bottom_img_top_right = (ImageView) contentView.findViewById(R.id.home_bottom_img_top_right);
        home_bottom_txt_top_right = (TextView) contentView.findViewById(R.id.home_bottom_txt_top_right);
        home_bottom_img_bottom_left = (ImageView) contentView.findViewById(R.id.home_bottom_img_bottom_left);
        home_bottom_txt_bottom_left = (TextView) contentView.findViewById(R.id.home_bottom_txt_bottom_left);
        home_bottom_img_bottom_right = (ImageView) contentView.findViewById(R.id.home_bottom_img_bottom_right);
        home_bottom_txt_bottom_right = (TextView) contentView.findViewById(R.id.home_bottom_txt_bottom_right);


        bannerviewpagerview = (BannerViewPager) contentView.findViewById(R.id.bannerviewpagerview);
        bannerViewPagerAdapter = new BannerViewPagerAdapter(bannerArrayList, new OnPageClickListener() {
            @Override
            public void onPageClick(View view, int position) {

            }
        });


        home_item_image_jyf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TestDetailActivity.launch((AppCompatActivity) getActivity(), view, R.mipmap.bof);
            }
        });

        bannerviewpagerview.setAdapter(bannerViewPagerAdapter);


        home_top_item_design = (LinearLayout) contentView.findViewById(R.id.home_top_item_design);
        home_top_item_design.setOnClickListener(onClickListener);
        home_top_item_company = (LinearLayout) contentView.findViewById(R.id.home_top_item_company);
        home_top_item_company.setOnClickListener(onClickListener);
        home_top_item_knowledge = (LinearLayout) contentView.findViewById(R.id.home_top_item_knowledge);
        home_top_item_knowledge.setOnClickListener(onClickListener);
        home_top_item_offer = (LinearLayout) contentView.findViewById(R.id.home_top_item_offer);
        home_top_item_offer.setOnClickListener(onClickListener);
    }


    /**
     * 网络请求
     */
    public void getData() {
        String url = Configs.HOST + Configs.URL_DESIGN_INDEX;
        OkHttpConn.getOkhttp(getContext(), url, new OkHttpConn.OkHttpCallBack() {
            @Override
            public void success(Call call, Response response) throws IOException {
                final String value = response.body().string();
                L.i(value);
                configView(value);
            }

            @Override
            public void fail(Call call, IOException e) {

            }
        });
    }

    /**
     * 数据设置
     *
     * @param value
     */
    public void configView(String value) {
        if (value.isEmpty())
            return;
        homeBean = new Gson().fromJson(value, HomeBean.class);
        if (homeBean != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    configBannerViewPagerView(homeBean.data.banner);

                    home_bottom_txt_top_left.setText(homeBean.data.company.get(0).company_name);
                    home_bottom_txt_top_right.setText(homeBean.data.company.get(1).company_name);
                    home_bottom_txt_bottom_left.setText(homeBean.data.company.get(2).company_name);
                    home_bottom_txt_bottom_right.setText(homeBean.data.company.get(3).company_name);

                    L.i(homeBean.data.company.get(0).logo);
                    L.i(homeBean.data.company.get(1).logo);
                    L.i(homeBean.data.company.get(2).logo);
                    L.i(homeBean.data.company.get(3).logo);

                    Glide.with(getActivity())
                            .load(homeBean.data.company.get(0).logo)
                            .centerCrop()
                            .placeholder(R.mipmap.jyf)
                            .into(home_bottom_img_top_left);

                    Glide.with(getActivity())
                            .load(homeBean.data.company.get(1).logo)
                            .centerCrop()
                            .placeholder(R.mipmap.jyf)
                            .into(home_bottom_img_top_right);

                    Glide.with(getActivity())
                            .load(homeBean.data.company.get(2).logo)
                            .centerCrop()
                            .placeholder(R.mipmap.jyf)
                            .into(home_bottom_img_bottom_left);

                    Glide.with(getActivity())
                            .load(homeBean.data.company.get(3).logo)
                            .centerCrop()
                            .placeholder(R.mipmap.jyf)
                            .into(home_bottom_img_bottom_right);
                }
            });
        }
    }


    /**
     * 设置banner 布局
     */
    public void configBannerViewPagerView(List<HomeBean.DataBean.BannerBean> arrayList) {
        if (arrayList != null) {
            int size = arrayList.size();
            ImageView imageView = null;
            for (int i = 0; i < size; i++) {
                HomeBean.DataBean.BannerBean bannerBean = arrayList.get(i);
                imageView = (ImageView) LayoutInflater.from(getContext())
                        .inflate(R.layout.banner_item, bannerviewpagerview, false);

                Glide.with(getActivity())
                        .load(ToolUtils.getImageUrl(bannerBean.img))
                        .into(imageView);
                bannerArrayList.add(imageView);
            }
            bannerViewPagerAdapter.notifyDataSetChanged();
        }
    }


    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.home_top_item_design:
                    startActivity(new Intent(getActivity(), DesignImageActivity.class));
                    break;
                case R.id.home_top_item_company:

                    break;
                case R.id.home_top_item_knowledge:

                    break;
                case R.id.home_top_item_offer:

                    break;
            }
        }
    };


    @Override
    public void onResume() {
        super.onResume();
        gyroscopeObserver.register(getActivity());
    }

    @Override
    public void onPause() {
        super.onPause();
        gyroscopeObserver.unregister();
    }
}
